package com.wushijia.java8.lambda;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

import com.wushijia.model.Trader;
import com.wushijia.model.Transaction;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * description:练习题
 *
 * @author yang
 * @date 2018/5/1 7:21
 */
public class TransactionTest {

  public static void main(String[] args) {
    Trader raoul = new Trader("Raoul", "Cambridge");
    Trader mario = new Trader("Mario", "Milan");
    Trader alan = new Trader("Alan", "Cambridge");
    Trader brian = new Trader("Brian", "Cambridge");
    List<Transaction> transactions = Arrays.asList(
        new Transaction(brian, 2011, 300),
        new Transaction(raoul, 2012, 1000),
        new Transaction(raoul, 2011, 400),
        new Transaction(mario, 2012, 710),
        new Transaction(mario, 2012, 700),
        new Transaction(alan, 2012, 500));
    //找出2011年发生的所有交易，并按交易额排序（从低到高）
    List tran2011 = transactions.stream().filter(tran -> 2011 == tran.getYear()).sorted(comparing(Transaction::getValue)).collect(toList());
    System.out.println(tran2011);

    //交易员都在哪些城市工作过
    List allCity = transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct().collect(Collectors.toList());
    System.out.println(allCity);
    Set allCity2 = transactions.stream().map(transaction -> transaction.getTrader().getCity()).collect(toSet());
    System.out.println(allCity2);

    //查找所有来自剑桥的交易员，并按姓名排序
    List camBridgeTrader = transactions.stream().map(Transaction::getTrader)
        .filter(transaction -> "Cambridge".equals(transaction.getCity())).sorted(comparing(Trader::getName)).distinct().collect(toList());
    System.out.println(camBridgeTrader);

    //返回所有交易员的姓名字符串，并按字母顺序排序
    transactions.stream().map(transaction -> transaction.getTrader().getName()).distinct().sorted().collect(toList()).forEach(System.out::println);
    String allTrader2 = transactions.stream().map(transaction -> transaction.getTrader().getName()).distinct().sorted().collect(joining(","));
    System.out.println(allTrader2);

    //有没有交易员是在米兰工作的
    transactions.stream().filter(tran -> "Milan".equals(tran.getTrader().getCity())).map(transaction -> transaction.getTrader().getName()).findAny()
        .ifPresent(System.out::println);
    boolean isInMilan = transactions.stream().anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));
    System.out.println(isInMilan);

    //打印生活在剑桥的交易员的所有交易额
    int transSum = transactions.stream().filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity())).map(Transaction::getValue)
        .reduce(0, (x, y) -> x + y);
    System.out.println(transSum);
    Optional transSumOptional = transactions.stream().filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity()))
        .map(Transaction::getValue).reduce(Integer::sum);
    if (transSumOptional.isPresent()) {
      System.out.println(transSumOptional.get());
    }

    //mapToInt返回一个IntStream(数值流),调用sum方法，还有max,min,average等
    IntStream intStream = transactions.stream().mapToInt(Transaction::getValue);
    System.out.println(intStream.sum());
    //Stream<Integer> stream = intStream.boxed();//将数值流转换为stream

    //所有交易中，最高的交易额是多少
    int maxTranValue = transactions.stream().max(comparing(Transaction::getValue)).get().getValue();
    System.out.println(maxTranValue);

    //默认值OptionalInt,如果不存在，可以给个默认值
    OptionalInt optionalInt = transactions.stream().mapToInt(Transaction::getValue).max();
    System.out.println(optionalInt.orElse(1));

    //找到交易额最小的交易
    int minTranValue = transactions.stream().min(comparing(Transaction::getValue)).get().getValue();
    System.out.println(minTranValue);
  }
}
